package com.julian.orderPattern;

public class RemoteControlWithUndo {
    Command[] onCommands;
    Command[] offCommands;
    Command preCommand;

    public RemoteControlWithUndo() {
        this.onCommands = new Command[7];
        this.offCommands = new Command[7];
        Command noCommand = new NoCommand();
        for (int i = 0; i < 7; i++){
            offCommands[i] = noCommand;
            onCommands[i] = noCommand;
        }
        preCommand = noCommand;
    }

    public void setCommands(int slot, Command onCommand, Command offCommand){
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }
    public void onButtonWasPushed(int slot){
        onCommands[slot].execute();
        preCommand = onCommands[slot];
    }
    public void offButtonWasPushed(int slot){
        offCommands[slot].execute();
        preCommand = offCommands[slot];
    }

    public void undo(){
        preCommand.undo();
    }

    @Override
    public String toString(){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("\n----- Remote Control ----- \n");
        for(int i = 0 ; i< onCommands.length; i++){
            stringBuffer.append("[slot" + i + "]" + onCommands[i].getClass().getName()
            + "    " + offCommands[i].getClass().getName() + "\n");
        }
        stringBuffer.append(preCommand.getClass().getName() + "\n");
        return stringBuffer.toString();
    }
}
